//
//  XDProblemRecordViewController.m
//  XDEducation
//
//  Created by 星道 on 17/5/15.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDProblemRecordViewController.h"
#import "XDProblemRecordCell.h"
#import "XDProblemRecordModel.h"

@interface XDProblemRecordViewController ()
@property (nonatomic, assign) NSInteger Index;
@property (nonatomic, strong) NSMutableArray *DataArr;
@end

@implementation XDProblemRecordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.separatorStyle = NO;
    
    self.Index = 1;
    
    [self ConfigTable];
    
}


#pragma mark - TableViewConfig
/** 配置表单 */
- (void)ConfigTable
{
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [self RefreshData];
    }];
    [self.tableView registerNib:[UINib nibWithNibName:[XDProblemRecordCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    
    [self.tableView.mj_footer beginRefreshing];
}



#pragma mark - 网络请求
- (void)RefreshData
{
    [XDHttpTool get:BaseURL(@"exercise.do?action=getPaperResultB") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:@{@"pageNumber":@(self.Index)} success:^(id responseObj) {
        
        [self.tableView.mj_footer endRefreshing];
        if ([responseObj[@"dataList"] count] > 0)
        {
            for (NSDictionary *Dict in responseObj[@"dataList"])
            {
                [self.DataArr addObject:[[XDProblemRecordModel alloc] initWithDict:Dict]];
            }
            self.Index += 1;
            [self.tableView reloadData];
                
        }
        else
        {
            if (self.Index > 1)
            {
                self.Index -= 1;
            }
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSError *error) {
        if (self.Index > 1)
        {
            self.Index -= 1;
        }
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD showError:XDNetWorkError];
    }];
}




#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.DataArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDProblemRecordCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.Model = self.DataArr[indexPath.row];
    return Cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 192;
}


#pragma mark - Setter
- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}
@end
